package com.escom.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.escom.R;
import com.escom.healthlc.entidad.TemperaturaORM;
import com.escom.service.TemperaturaService;
import com.escom.utils.CustomTemperaturaAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by Carlos on 22/04/2014.
 */
public class TemperaturaFragment extends Fragment {

    private ListView listViewTemperatura;
    private CustomTemperaturaAdapter customTemperaturaAdapter;
    private static Thread tareaTemperatura;

    private class TemperaturaObservador implements Observer {

        @Override
        public void update(Observable o, Object arg) {
            customTemperaturaAdapter.notifyDataSetChanged();
        }
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View viewActivity = inflater.inflate(R.layout.fragment_temperatura, container, false);

        listViewTemperatura = (ListView) viewActivity.findViewById(R.id.listViewTemperatura);

        if (tareaTemperatura != null && tareaTemperatura.isAlive()){
            //Log.d(Constantes.LOG_ID, this.getClass().getName() + " El hilo ya existe");
        }else{
            List<TemperaturaORM> temperaturaList = new ArrayList<TemperaturaORM>();
            customTemperaturaAdapter = new CustomTemperaturaAdapter(viewActivity.getContext(), R.layout.custom_temperatura, temperaturaList);
            listViewTemperatura.setAdapter(customTemperaturaAdapter);

            Observer temperaturaObservador = new TemperaturaObservador();
            TemperaturaService temperaturaService = new TemperaturaService(customTemperaturaAdapter, getActivity(), temperaturaList);
            temperaturaService.addObserver(temperaturaObservador);

            tareaTemperatura = new Thread(temperaturaService);
            tareaTemperatura.start();
            //Log.d(Constantes.LOG_ID, this.getClass().getName() + " Se inicio el hilo");
        }
        return viewActivity;
    }

}
